﻿/*
 * Implementacja notyfikacji na podstawie "Custom soft key Notifications" by Christopher Fairbairn http://www.christec.co.nz/blog/archives/150
 */

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;

namespace PocketGPW
{
    /// <summary>
    /// Handles messages received from the Notification system and throws events in the parent NotificationEngine object
    /// </summary>
    internal class NotificationMessageWindow : Microsoft.WindowsCE.Forms.MessageWindow
    {
        private Dictionary<int, Notification> _notifications;

        public NotificationMessageWindow(Dictionary<int, Notification> notifications)
        {
            _notifications = notifications;
        }

        protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message m)
        {
            switch (m.Msg)
            {
                //WM_NOTIFY
                case 78:
                    NMSHN nm = (NMSHN)Marshal.PtrToStructure(m.LParam, typeof(NMSHN));
                    Notification n = (Notification)_notifications[nm.idFrom];

                    switch (nm.code)
                    {
                        case SHNN.DISMISS:
                            n.OnBalloonChanged(new BalloonChangedEventArgs(false));
                            break;
                        case SHNN.SHOW:
                            n.OnBalloonChanged(new BalloonChangedEventArgs(true));
                            break;
                        case SHNN.LINKSEL:
                            string link = Marshal.PtrToStringUni((IntPtr)nm.union1);
                            n.OnResponseSubmitted(new ResponseSubmittedEventArgs(link));
                            break;
                        case SHNN.NAVNEXT:
                            n.OnSpinnerClick(new NotificationSpinnerClickEventArgs(true));
                            break;
                        case SHNN.NAVPREV:
                            n.OnSpinnerClick(new NotificationSpinnerClickEventArgs(false));
                            break;
                    }
                    break;

                //WM_COMMAND
                case 0x0111:
                    {
                        uint value = ((uint)m.WParam & 0xFFFF);
                        int id = (int)(value >> 8);
                        byte index = (byte)(value & 0xFF);

                        Notification z = (Notification)_notifications[id];
                        if (index == 0)
                            z.OnLeftSoftKeyClick(EventArgs.Empty);
                        else
                            z.OnRightSoftKeyClick(EventArgs.Empty);
                    }
                    break;
            }

            //do base wndproc
            base.WndProc(ref m);
        }

    }
}
